<?php

namespace App\Http\Controllers\Backstage;

use App\Http\Controllers\Backstage\BCommonController;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;
use App\Models\BackstageModel;
use App\Models\CommonModel;
use App\Models\UserModel;
use Storage;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Libary\Captcha\Code;

// require_once '../app/libary/captcha/Code.class.php';

class User_moneylogController extends BCommonController
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/backstage/user_moneylog/index';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    // public function __construct()
    // {
    //     $this->middleware('guest', ['except' => 'logout']);
    // }

    public function index(){
        $map='';
        $search=array();
        $field = 'l.id,l.uid,u.user_name,u.user_email,l.type,l.affect_money,l.collect_money,l.freeze_money,l.account_money,l.add_time,l.info';
        if(!empty($_REQUEST['uname'])){
            $map.=" u.user_name LIKE '%".trim($_REQUEST['uname'])."%'  AND ";
            $search['uname'] = $_REQUEST['uname'];
        }
        if(!empty($_REQUEST['bj']) && !empty($_REQUEST['money'])){
            $map.='l.affect_money'.$_REQUEST['bj'].trim($_REQUEST['money']).' AND ';
            $search['bj'] = $_REQUEST['bj'];
            $search['money'] = $_REQUEST['money'];
        }
        if(!empty($_REQUEST['start_time']) && !empty($_REQUEST['end_time'])){
            $map.='l.add_time BETWEEN'.strtotime(trim($_REQUEST['start_time'])).' AND '.strtotime(trim($_REQUEST['end_time']));
            $search['start_time'] = $_REQUEST['start_time'];
            $search['end_time'] = $_REQUEST['end_time'];
        }
        if(!empty($_REQUEST['start_time'])){
            $map.='l.add_time>'.strtotime(trim($_REQUEST['start_time'])).' AND ';
            $search['start_time'] = $_REQUEST['start_time'];
        }
        if(!empty($_REQUEST['end_time'])){
            $map.='l.add_time<'.strtotime(trim($_REQUEST['end_time'])).' AND ';
            $search['end_time'] = $_REQUEST['end_time'];
        }
        if(!empty($_REQUEST['type'])){
            $map.= 'l.type='.intval($_REQUEST['type']).' AND ';
            $search['type'] = $_REQUEST['type'];
        }
        $map.=' 1=1';
        // DB::enableQueryLog();
        $list = BackstageModel::getMoneyLogList($field,$map);
        // $queries = DB::getQueryLog();
        // dd($queries);
        // dd($list);
        $trade_type = config('app.trade_type');
        // dd($list);
        return view('backstage/user_moneylog/index',['list'=>$list,'trade_type'=>$trade_type,'search'=>$search,"query"=>http_build_query($search)]);
    }
    
    
    // 查看會員詳細信息
    public function show($id)
    {
        $table = 'user';
        $data =['id'=>$id];
        $user = UserModel::getOneUser($data);
        // dd($user);
        return view('backstage/common/user',['user'=>$user]);
    }
    


}
